<?php
/*
 * @Description: Bark消息推送
 * @Version: 1.0
 * @Author: beiwangshan
 * @Date: 2023-03-07 23:40:05
 * @LastEditors: beiwangshan
 * @LastEditTime: 2023-03-12 21:55:55
 */

function zeo_bark($key, $title, $content, $url = '', $isActive = 1, $autoCopy = 1)
{
    $bark_api = 'https://api.day.app/' . $key;
    $headers = array(
        'Content-Type' => 'application/x-www-form-urlencoded'
    );
    $body =  array(
        'isActive' => $isActive,
        'autoCopy' => $autoCopy,
        'title' => $title,
        'body' => $content,
        'copy' => $url,
    );
    $data = EL_HTTP::doPost($bark_api, array(
        'headers' => $headers,
        'body' => $body,
        'timeout' => 30,
    ));
    return json_encode($data);
}

/**
 * 注册 初始化函数 到 admin_init Action 钩子
 */
// add_action('wp_insert_comment', 'zeo_bark_comment', 10, 2);
function zeo_bark_comment($comment_id, $comment_object)
{
    $title = "您的博客收到了新的评论";
    $comment_post_ID = $comment_object->comment_post_ID;
    $post_url = get_permalink($comment_post_ID);
    $content = $comment_object->comment_author . ": " . $comment_object->comment_content;
    zeo_bark('qkkfKA34unMgNX8u6eqpT8', $title, $content, $post_url, '博客评论消息');
}
